/**
 * 
 */
package servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import recipe.Ingredient;
import recipe.Recipe;

import bean.BrowseRecipeBean;
import bean.SearchRecipeBean;
import project.taip.wawwe.db.Services;

/**
 * @author Andrei Iordache
 *
 */
public class BrowseRecipeServlet extends HttpServlet implements RecipeServlet {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request,
            HttpServletResponse response) {
        processPostGetRequest(request, response);
    }

    public void doPost(HttpServletRequest request,
            HttpServletResponse response) {
        processPostGetRequest(request, response);
    }

    /* (non-Javadoc)
     * @see servlet.RecipeServlet#processPostGetRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public void processPostGetRequest(HttpServletRequest request,
            HttpServletResponse response) {
        BrowseRecipeBean bean = (BrowseRecipeBean) request.getAttribute("instance");
        String letter = bean.getLetter();
        StringBuilder sB = new StringBuilder();
        if ("all".equals(letter)) {
            ArrayList<project.taip.wawwe.model.Recipe> array = Services.browseRecipes("");
            for (project.taip.wawwe.model.Recipe r : array) {
                sB.append("<br>");
                sB.append("<b>Name: </b>");
                sB.append("<br>");
                sB.append(r.getName());
                sB.append("<br>");
                sB.append("<br>");
                sB.append("<b>Ingredients: </b>");
                sB.append("<br>");
                for (project.taip.wawwe.model.Ingredient i : r.getIngredients()) {
                    sB.append(i.getQuantity() + " " + i.getUnit() + " " + i.getName());
                    sB.append("<br>");
                }
                sB.append("<br>");
                sB.append("<b>Description: </b>");
                sB.append("<br>");
                sB.append(r.getDescription());
                sB.append("<br>");
                sB.append("<br>");
            }
        } else {
            ArrayList<project.taip.wawwe.model.Recipe> array = Services.browseRecipes(letter);
            for (project.taip.wawwe.model.Recipe r : array) {
                if (r.getName().toLowerCase().startsWith(letter)) {
                     sB.append("<br>");
                sB.append("<b>Name: </b>");
                sB.append("<br>");
                sB.append(r.getName());
                sB.append("<br>");
                sB.append("<br>");
                sB.append("<b>Ingredients: </b>");
                sB.append("<br>");
                for (project.taip.wawwe.model.Ingredient i : r.getIngredients()) {
                    sB.append(i.getQuantity() + " " + i.getUnit() + " " + i.getName());
                    sB.append("<br>");
                }
                sB.append("<br>");
                sB.append("<b>Description: </b>");
                sB.append("<br>");
                sB.append(r.getDescription());
                sB.append("<br>");
                sB.append("<br>");
                }
            }
        }
        bean.setResult(sB.toString());
        try {
            getServletContext().getRequestDispatcher("/browse.jsp?process=false").
                    forward(request, response);
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


